
import 'package:badges/badges.dart';
import 'package:flutter/material.dart';

class SimpleImageButton extends StatefulWidget {
  //图标
  String icon;
  //标题
  String title;
  double width;
  Color color;
  Function onPressed;
  bool showBadge;
  //文本与图片之间的距离
  double padding;
  TextStyle style;
  String badge;

  SimpleImageButton({
    Key key,
    @required this.icon,
    @required this.onPressed,
    @required this.title,
    this.badge,
    this.showBadge = false,
    this.color,
    this.padding,
    this.style,
    this.width,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _SimpleImageButtonState();
  }
}

class _SimpleImageButtonState extends State<SimpleImageButton> {
  bool isPressed = false;
  @override
  Widget build(BuildContext context) {
    double padding = widget.padding == null ? 8 : widget.padding;
    double width = widget.width == null ? 32 : widget.width;
    TextStyle style = widget.style == null
        ? TextStyle(fontSize: 14, color: Colors.black)
        : widget.style;
    Color colorBg = widget.color??Colors.transparent;
    String badge = widget.badge??'0';
    bool showBadge = widget.showBadge?? false;
    return  GestureDetector(
      child: Container(
        color: colorBg,
        child: Badge(
          showBadge: showBadge,
          badgeContent: Text(badge,style: TextStyle(color: Colors.white,fontSize: 12),),
          child: Center(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                Image(
                  image: AssetImage(widget.icon),
                  width: width,
                  height: width,
                ),
                widget.padding != null
                    ? Padding(padding: EdgeInsets.fromLTRB(0, padding, 0, 0))
                    : Container(),
                Text(
                  widget.title,
                  style: style,
                )
              ],
            ),
          ),
        ),
        foregroundDecoration: BoxDecoration(
          color:
          isPressed ? Colors.white.withOpacity(0.4) : Colors.transparent,
        ),
      ),
      onTap: () => widget.onPressed(),
      onTapDown: (d) => setState(() => this.isPressed = true),
      onTapUp: (d) => setState(() => this.isPressed = false),
      onTapCancel: () => setState(() => this.isPressed = false),
    );
  }
}
